home *** CD-ROM | disk | FTP | other *** search
/ Workbench Design / WB Collection.iso / workbench werkzeuge / memory & system tools / setbatt / setbatt.c < prev    next >
C/C++ Source or Header  |  1996-04-07  |  11KB  |  414 lines

  1. /*
  2.    SetBatt, see following two include files for complete description
  3.             of possible flags.  Don't modify any flags if you're not
  4.             ABSOLUTELY SURE what you're doing.  This is NO JOKE!
  5. */
  6.  
  7. #include <resources/battmembitsshared.h>
  8. #include <resources/battmembitsamiga.h>
  9.  
  10. #include <resources/battmem.h>
  11. #include <exec/execbase.h>
  12. #include <dos/dosextens.h>
  13. #include <exec/memory.h>
  14. #include <dos/rdargs.h>
  15.  
  16. #include <proto/dos.h>
  17. #include <proto/exec.h>
  18. #include <proto/battmem.h>
  19.  
  20.     /* Argument template and extra help. */
  21.  
  22. #define ARG_TEMPLATE    "T=Timeout/K,U=UseLUNS/K,H=HostID/K/N,S=Sync/K,F=Fast/K,Q=Queue/K,A=Amnesia/S,AAmiga/K,AShared/K"
  23.  
  24. #define ARG_HELP    "\nUsage: SetBatt [Timeout L|S] [UseLUNS Y|N] [HostID n] [Sync Y|N]\n\
  25.                [Fast Y|N] [Queue Y|N] [Amnesia] [AAmiga Y|N] [AShared Y|N]\n\n\
  26.                Timeout Set (L)ong or (S)hort SCSI selection timeout.\n\
  27.                UseLUNS Access logical units above 0 at any given\n\
  28.                        SCSI address, responds to (Y)es or (N)o.\n\
  29.                HostID  Set the SCSI controller's host ID,\n\
  30.                        correct IDs are 0..7.\n\
  31.                Sync    Initiate synchronous transfer, responds to\n\
  32.                        (Y)es or (N)o.\n\
  33.                Fast    Initiate fast synchronous transfer, responds to\n\
  34.                        (Y)es or (N)o.\n\
  35.                Queue   Enable SCSI-2 tagged queuing, responds to\n\
  36.                        (Y)es or (N)o.\n\
  37.                Amnesia Reset amnesia flag (memory loss acknowledged)\n\
  38.                AAmiga  battery-backedup memory has had a memory loss\n\
  39.                        on the Amiga side, (Y)es or (N)o.\n\
  40.                AShared battery-backedup memory has had a memory loss\n\
  41.                        on the shared Amiga/Unix side, (Y)es or (N)o.\n\n"
  42.  
  43. #define ARG_MAXNUM 9
  44.  
  45.     /* Argument vector offsets. */
  46.  
  47. enum {
  48.     ARG_TIMEOUT, ARG_USELUNS, ARG_HOSTID, ARG_SYNC, ARG_FAST,
  49.     ARG_QUEUE, ARG_AMNESIA, ARG_AAMIGA, ARG_ASHARED
  50. };
  51.  
  52.     /* Handy international toupper() macro. */
  53.  
  54. #define ToUpper(c)    ((((c) >= 224 && (c) <= 254) || ((c) >= 'a' && (c) <= 'z')) ? (c) - 32 : (c))
  55.  
  56.     /* Shared library identifiers. */
  57.  
  58. extern struct ExecBase *SysBase;
  59. extern struct DosLibrary *DOSBase;
  60.  
  61. struct Library *BattMemBase;
  62.  
  63.     /* Version string. */
  64.  
  65. static char setbattver[] = "\0$VER: SetBatt 1.2 (22.5.94)";
  66.  
  67.     /* Prototypes for this module. */
  68.  
  69. void ShowConfig (void);
  70.  
  71.     /* Main():
  72.  
  73.      *      Main entry point to this module.
  74.      */
  75.  
  76. int main (void)
  77. {
  78.     LONG Result = RETURN_FAIL;
  79.  
  80.     /* Set up SysBase. */
  81.  
  82.     /* SysBase = *(struct ExecBase **) 4; */
  83.     /* SysBase = (struct ExecBase *)OpenLibrary("exec.library",37); */
  84.  
  85.     /* Open dos.library. */
  86.  
  87.     if (DOSBase = (struct DosLibrary *) OpenLibrary ("dos.library", 37)) {
  88.         /* Open battmem.resource. */
  89.  
  90.         if (BattMemBase = (struct Library *) OpenResource (BATTMEMNAME)) {
  91.             struct RDArgs *ArgsPtr;
  92.             UBYTE **Args;
  93.  
  94.             /* Allocate space for the RDArgs auxilary structure. */
  95.  
  96.             if (ArgsPtr = AllocVec (sizeof (struct RDArgs), MEMF_PUBLIC | MEMF_CLEAR)) {
  97.                 /* Provide extended help information. */
  98.  
  99.                 ArgsPtr->RDA_ExtHelp = ARG_HELP;
  100.  
  101.                 /* Allocate space for the arguments. */
  102.  
  103.                 if (Args = (UBYTE **) AllocVec (sizeof (UBYTE *) * ARG_MAXNUM, MEMF_PUBLIC | MEMF_CLEAR)) {
  104.                     /* Read the args if any. */
  105.  
  106.                     if (ReadArgs (ARG_TEMPLATE, (APTR) Args, ArgsPtr)) {
  107.                         UBYTE Data;
  108.  
  109.                         Result = RETURN_OK;
  110.  
  111.                         /* Obtain exclusive access
  112.                          * to battmem.resource.
  113.                          */
  114.  
  115.                         ObtainBattSemaphore ();
  116.  
  117.                         /* Set the SCSI scan timeout. */
  118.  
  119.                         if (Args[ARG_TIMEOUT]) {
  120.                             switch (ToUpper (Args[ARG_TIMEOUT][0])) {
  121.                             case 'L':
  122.                                 Data = 1;
  123.                                 WriteBattMem (&Data, BATTMEM_SCSI_TIMEOUT_ADDR, BATTMEM_SCSI_TIMEOUT_LEN);
  124.                                 break;
  125.  
  126.                             case 'S':
  127.                                 Data = 0;
  128.                                 WriteBattMem (&Data, BATTMEM_SCSI_TIMEOUT_ADDR, BATTMEM_SCSI_TIMEOUT_LEN);
  129.                                 break;
  130.  
  131.                             default:
  132.                                 Printf ("SetBatt: Argument not recognized \"%s\".\n", Args[ARG_TIMEOUT]);
  133.                                 Result = RETURN_FAIL;
  134.                                 break;
  135.                             }
  136.                         }
  137.  
  138.                         /* Toggle LUNs. */
  139.  
  140.                         if (Result != RETURN_FAIL) {
  141.                             if (Args[ARG_USELUNS]) {
  142.                                 switch (ToUpper (Args[ARG_USELUNS][0])) {
  143.                                 case 'Y':
  144.                                     Data = 1;
  145.                                     WriteBattMem (&Data, BATTMEM_SCSI_LUNS_ADDR, BATTMEM_SCSI_LUNS_LEN);
  146.                                     break;
  147.  
  148.                                 case 'N':
  149.                                     Data = 0;
  150.                                     WriteBattMem (&Data, BATTMEM_SCSI_LUNS_ADDR, BATTMEM_SCSI_LUNS_LEN);
  151.                                     break;
  152.  
  153.                                 default:
  154.                                     Printf ("SetBatt: Argument not recognized \"%s\".\n", Args[ARG_USELUNS]);
  155.                                     Result = RETURN_FAIL;
  156.                                     break;
  157.                                 }
  158.                             }
  159.                         }
  160.  
  161.                         /* Select SCSI host ID. */
  162.  
  163.                         if (Result != RETURN_FAIL) {
  164.                             if (Args[ARG_HOSTID]) {
  165.                                 LONG ID = *((LONG *) Args[ARG_HOSTID]);
  166.  
  167.                                 if (ID < 0 || ID > 7) {
  168.                                     Printf ("SetBatt: SCSI Host ID must be between 0 and 7.\n");
  169.                                     Result = RETURN_FAIL;
  170.                                 }
  171.                                 else {
  172.                                     Data = ID ^ 7;
  173.                                     WriteBattMem (&Data, BATTMEM_SCSI_HOST_ID_ADDR, BATTMEM_SCSI_HOST_ID_LEN);
  174.                                 }
  175.                             }
  176.                         }
  177.  
  178.                         /* Toggle (general) synchronous transfer. */
  179.  
  180.                         if (Result != RETURN_FAIL) {
  181.                             if (Args[ARG_SYNC]) {
  182.                                 switch (ToUpper (Args[ARG_SYNC][0])) {
  183.                                 case 'Y':
  184.                                     Data = 1;
  185.                                     WriteBattMem (&Data, BATTMEM_SCSI_SYNC_XFER_ADDR, BATTMEM_SCSI_SYNC_XFER_LEN);
  186.                                     break;
  187.  
  188.                                 case 'N':
  189.                                     Data = 0;
  190.                                     WriteBattMem (&Data, BATTMEM_SCSI_SYNC_XFER_ADDR, BATTMEM_SCSI_SYNC_XFER_LEN);
  191.                                     break;
  192.  
  193.                                 default:
  194.                                     Printf ("SetBatt: Argument not recognized \"%s\".\n", Args[ARG_SYNC]);
  195.                                     Result = RETURN_FAIL;
  196.                                     break;
  197.                                 }
  198.                             }
  199.                         }
  200.  
  201.                         /* Toggle FAST synchronous transfer. */
  202.  
  203.                         if (Result != RETURN_FAIL) {
  204.                             if (Args[ARG_FAST]) {
  205.                                 switch (ToUpper (Args[ARG_FAST][0])) {
  206.                                 case 'Y':
  207.                                     Data = 0;
  208.                                     WriteBattMem (&Data, BATTMEM_SCSI_FAST_SYNC_ADDR, BATTMEM_SCSI_FAST_SYNC_LEN);
  209.                                     break;
  210.  
  211.                                 case 'N':
  212.                                     Data = 1;
  213.                                     WriteBattMem (&Data, BATTMEM_SCSI_FAST_SYNC_ADDR, BATTMEM_SCSI_FAST_SYNC_LEN);
  214.                                     break;
  215.  
  216.                                 default:
  217.                                     Printf ("SetBatt: Argument not recognized \"%s\".\n", Args[ARG_FAST]);
  218.                                     Result = RETURN_FAIL;
  219.                                     break;
  220.                                 }
  221.                             }
  222.                         }
  223.  
  224.                         /* Toggle SCSI-2 tagged queuing. */
  225.  
  226.                         if (Result != RETURN_FAIL) {
  227.                             if (Args[ARG_QUEUE]) {
  228.                                 switch (ToUpper (Args[ARG_QUEUE][0])) {
  229.                                 case 'Y':
  230.                                     Data = 1;
  231.                                     WriteBattMem (&Data, BATTMEM_SCSI_TAG_QUEUES_ADDR, BATTMEM_SCSI_TAG_QUEUES_LEN);
  232.                                     break;
  233.  
  234.                                 case 'N':
  235.                                     Data = 0;
  236.                                     WriteBattMem (&Data, BATTMEM_SCSI_TAG_QUEUES_ADDR, BATTMEM_SCSI_TAG_QUEUES_LEN);
  237.                                     break;
  238.  
  239.                                 default:
  240.                                     Printf ("SetBatt: Argument not recognized \"%s\".\n", Args[ARG_QUEUE]);
  241.                                     Result = RETURN_FAIL;
  242.                                     break;
  243.                                 }
  244.                             }
  245.                         }
  246.  
  247.                         /* Amnesia, reset all flags. */
  248.  
  249.                         if (Result != RETURN_FAIL) {
  250.                             if (Args[ARG_AMNESIA]) {
  251.                                 Data = 1;
  252.                                 WriteBattMem (&Data, BATTMEM_AMIGA_AMNESIA_ADDR, BATTMEM_AMIGA_AMNESIA_LEN);
  253.                                 Data = 1;
  254.                                 WriteBattMem (&Data, BATTMEM_SHARED_AMNESIA_ADDR, BATTMEM_SHARED_AMNESIA_LEN);
  255.                             }
  256.                         }
  257.  
  258.                         /* Amnesia, Amiga side. */
  259.  
  260.                         if (Result != RETURN_FAIL) {
  261.                             if (Args[ARG_AAMIGA]) {
  262.                                 switch (ToUpper (Args[ARG_AAMIGA][0])) {
  263.                                 case 'Y':
  264.                                     Data = 0;
  265.                                     WriteBattMem (&Data, BATTMEM_AMIGA_AMNESIA_ADDR, BATTMEM_AMIGA_AMNESIA_LEN);
  266.                                     break;
  267.  
  268.                                 case 'N':
  269.                                     Data = 1;
  270.                                     WriteBattMem (&Data, BATTMEM_AMIGA_AMNESIA_ADDR, BATTMEM_AMIGA_AMNESIA_LEN);
  271.                                     break;
  272.  
  273.                                 default:
  274.                                     Printf ("SetBatt: Argument not recognized \"%s\".\n", Args[ARG_AAMIGA]);
  275.                                     Result = RETURN_FAIL;
  276.                                     break;
  277.                                 }
  278.                             }
  279.                         }
  280.  
  281.                         /* Amnesia, shared Amiga/Unix side. */
  282.  
  283.                         if (Result != RETURN_FAIL) {
  284.                             if (Args[ARG_ASHARED]) {
  285.                                 switch (ToUpper (Args[ARG_ASHARED][0])) {
  286.                                 case 'Y':
  287.                                     Data = 0;
  288.                                     WriteBattMem (&Data, BATTMEM_SHARED_AMNESIA_ADDR, BATTMEM_SHARED_AMNESIA_LEN);
  289.                                     break;
  290.  
  291.                                 case 'N':
  292.                                     Data = 1;
  293.                                     WriteBattMem (&Data, BATTMEM_SHARED_AMNESIA_ADDR, BATTMEM_SHARED_AMNESIA_LEN);
  294.                                     break;
  295.  
  296.                                 default:
  297.                                     Printf ("SetBatt: Argument not recognized \"%s\".\n", Args[ARG_ASHARED]);
  298.                                     Result = RETURN_FAIL;
  299.                                     break;
  300.                                 }
  301.                             }
  302.                         }
  303.  
  304.                         /* Show the current configuration. */
  305.  
  306.                         if (Result != RETURN_FAIL)
  307.                             ShowConfig ();
  308.  
  309.                         /* Release access semaphore. */
  310.  
  311.                         ReleaseBattSemaphore ();
  312.  
  313.                         /* Start the cleanup. */
  314.  
  315.                         FreeArgs (ArgsPtr);
  316.                     }
  317.                     else
  318.                         PrintFault (IoErr (), "SetBatt");
  319.  
  320.                     FreeVec (Args);
  321.                 }
  322.                 else
  323.                     Printf ("SetBatt: Unable to allocate argument line!\n");
  324.  
  325.                 FreeVec (ArgsPtr);
  326.             }
  327.         }
  328.         else
  329.             Printf ("SetBatt: Failed to open \"%s\"!\n", BATTMEMNAME);
  330.  
  331.         CloseLibrary ((struct Library *) DOSBase);
  332.     }
  333.  
  334.     return (Result);
  335. }
  336.  
  337.  
  338.  
  339.     /* ShowConfig():
  340.  
  341.      *      Display the current battmem settings.
  342.      */
  343.  
  344. void ShowConfig (void)
  345. {
  346.     UBYTE Data;
  347.  
  348.     /* Amnesia on the Amiga side? */
  349.  
  350.     if (!ReadBattMem (&Data, BATTMEM_AMIGA_AMNESIA_ADDR, BATTMEM_AMIGA_AMNESIA_LEN)) {
  351.         if (!(Data & 1))
  352.             Printf ("\33[33mWarning\33[31m - a memory loss has occured on the Amiga side!\n\n");
  353.     }
  354.  
  355.     /* Long or short timeouts? */
  356.  
  357.     if (!ReadBattMem (&Data, BATTMEM_SCSI_TIMEOUT_ADDR, BATTMEM_SCSI_TIMEOUT_LEN)) {
  358.         if (Data & 1)
  359.             Printf ("The SCSI selection timeout is \33[33mLONG\33[31m (2 seconds).\n");
  360.         else
  361.             Printf ("The SCSI selection timeout is \33[33mSHORT\33[31m (128 milliseconds).\n");
  362.     }
  363.  
  364.     /* Scan LUNs? */
  365.  
  366.     if (!ReadBattMem (&Data, BATTMEM_SCSI_LUNS_ADDR, BATTMEM_SCSI_LUNS_LEN)) {
  367.         if (Data & 1)
  368.             Printf ("Logical SCSI units above 0 \33[33mARE\33[31m accessed.\n");
  369.         else
  370.             Printf ("Logical SCSI units above 0 \33[33mARE NOT\33[31m accessed.\n");
  371.     }
  372.  
  373.     Printf ("\n");
  374.  
  375.     /* Amnesia on the shared Amiga/Amix side? */
  376.  
  377.     if (!ReadBattMem (&Data, BATTMEM_SHARED_AMNESIA_ADDR, BATTMEM_SHARED_AMNESIA_LEN)) {
  378.         if (!(Data & 1))
  379.             Printf ("\33[33mWarning\33[31m - a memory loss has occured on the shared Amiga/Unix side!\n\n");
  380.     }
  381.  
  382.     /* Show SCSI host ID. */
  383.  
  384.     if (!ReadBattMem (&Data, BATTMEM_SCSI_HOST_ID_ADDR, BATTMEM_SCSI_HOST_ID_LEN))
  385.         Printf ("The SCSI controller's host ID is \33[33m%ld\33[31m.\n", (Data & 7) ^ 7);
  386.  
  387.     /* Synchronous transfer enabled? */
  388.  
  389.     if (!ReadBattMem (&Data, BATTMEM_SCSI_SYNC_XFER_ADDR, BATTMEM_SCSI_SYNC_XFER_LEN)) {
  390.         if (Data & 1)
  391.             Printf ("Synchronous transfer is \33[33mINITIATED\33[31m.\n");
  392.         else
  393.             Printf ("Synchronous transfer is \33[33mNOT INITIATED\33[31m.\n");
  394.     }
  395.  
  396.     /* Fast synchronous transfer enabled? */
  397.  
  398.     if (!ReadBattMem (&Data, BATTMEM_SCSI_FAST_SYNC_ADDR, BATTMEM_SCSI_FAST_SYNC_LEN)) {
  399.         if (Data & 1)
  400.             Printf ("Fast synchronous transfer is \33[33mNOT INITIATED\33[31m (<=5MB/s).\n");
  401.         else
  402.             Printf ("Fast synchronous transfer is \33[33mINITIATED\33[31m (>5MB/s).\n");
  403.     }
  404.  
  405.     /* SCSI-2 tagged queuing enabled? */
  406.  
  407.     if (!ReadBattMem (&Data, BATTMEM_SCSI_TAG_QUEUES_ADDR, BATTMEM_SCSI_TAG_QUEUES_LEN)) {
  408.         if (Data & 1)
  409.             Printf ("SCSI-2 tagged queuing is \33[33mENABLED\33[31m.\n");
  410.         else
  411.             Printf ("SCSI-2 tagged queuing is \33[33mDISABLED\33[31m.\n");
  412.     }
  413. }
  414.